import 'dart:io';
import 'package:audio_waveforms/audio_waveforms.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:smart_translator/app/event/DataCallBack.dart';
import '../modules/recordscriptDetail/controllers/recordscript_detail_controller.dart';
import '../routes/app_pages.dart';
import '../style/app_colors.dart';
import '../style/widget_style.dart';
import '../test/FixedHeaderScrollPage.dart';
import '../test/ScrollHideDemo2.dart';
import '../utils/CommonUtil.dart';
import 'CommonWidgets.dart';
class AppHeader {

  static Widget obtainHeaderWithoutRightPart (String title) {
       return InkWell(
         onTap: () {
           Get.back();
         },
         child: Container(
             padding: EdgeInsets.all(10.w),
             margin: EdgeInsets.only(top: 35.h),
             width: Get.width,
             child: Row(
               children: [
                 Container(
                   child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                   padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
                 ),
                 Expanded(child: Row(
                   mainAxisAlignment: MainAxisAlignment.center,
                   children: [
                     Container(
                       child: Text(title,style: AppStyleThemeData.PAGE_TITLE),
                     )
                   ],
                 ))

               ],
             )
         ),
       );
  }

  static Widget obtainHeaderWithoutRightPartCal (String title,calback) {
    return InkWell(
      onTap: () {
        Get.back();
        calback();
      },
      child: Container(
          padding: EdgeInsets.all(10.w),
          margin: EdgeInsets.only(top: 35.h),
          width: Get.width,
          child: Row(
            children: [
              Container(
                child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
              ),
              Expanded(child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    child: Text(title,style: AppStyleThemeData.PAGE_TITLE),
                  )
                ],
              ))

            ],
          )
      ),
    );


  }


  static Widget obtainHeaderWithoutTitleAndRightPart ({DataCallBack? callback}) {
    return GestureDetector(
      onTap: () {
        print('11111111, callback: ${callback}');
        if (callback != null) {
          callback.businessCallBack!('');
        } else {
          Get.back();
        }

      },
      child: Container(
          color: Colors.red.withValues(alpha: 0),
          padding: EdgeInsets.all(10.w),
          margin: EdgeInsets.only(top: 35.h),
          width: Get.width,
          child: Row(
            children: [
              Container(
                padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),

                child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
              ),
            ],
          )
      ),
    );


  }


  static Widget obtainHeaderWithoutTitleAndRightPartWithEdit(avatarUrl, name,supportEdit) {
    var domainUrl = CommonUtil.getPicPre();
    var isPic = avatarUrl != '';
    double radius = 60.0;
    return Container(
      padding: EdgeInsets.all(10.w),
      margin: EdgeInsets.only(top: 100.h),
      width: Get.width,
      child: Center(
          child:GestureDetector(
              onTap: () {
                if(supportEdit){
                  Get.toNamed(Routes.OFFLINE_MODELS);
                }
              },
              child:CircleAvatar(backgroundImage: isPic?NetworkImage('${domainUrl}${avatarUrl}'):AssetImage('images/icon_default_avatar_l.svg'),radius: radius)
              // child: isPic?CircleAvatar(backgroundImage: NetworkImage('${domainUrl}${avatarUrl}'), radius: radius):
              // CircleAvatar(child: Text(name[0]), radius: radius)
            // isPic
            //     ? CircleAvatar(backgroundImage: NetworkImage('${url}${avatarUrl}'), radius: radius)
            //     : CircleAvatar(child: Text(name[0]), radius: radius)
         )

            // child: Container(
            //   padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
            //   color: Colors.red.withValues(alpha: 0),
            //   child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
            // ),
      ),
    );
  }

  static Widget obtainHeaderForHomePage() {
    return Container(
      padding: EdgeInsets.only(left: 20.w,right: 20.w,top: 10.h,bottom: 10.h),
      margin: EdgeInsets.only(top: 35.h),
      width: Get.width,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
             Row(
               children: [
                 Container(
                   padding: EdgeInsets.only(top: 5.h,bottom: 5.h,left: 5.w,right: 30.h),
                   decoration: BoxDecoration(
                     boxShadow: [
                       CommonWidgets.getBoxShadow(),
                     ],
                     color:Colors.white,
                     borderRadius: BorderRadius.circular(32), // Optional: add border radius
                   ),
                   child: Row(
                     children: [
                       Image.asset('assets/images/product_device1.png',width: 31.w,height: 31.h),
                       SizedBox(width: 10.w,),
                       Column(
                         mainAxisSize: MainAxisSize.min,
                         children: [
                           Text('智译Pro',style: TextStyle(
                               color: Colors.black,
                               fontSize: 12.sp
                           ),),
                           SizedBox(height: 5.h,),
                           Row(
                             children: [
                               SvgPicture.asset('assets/images/icon_index_header_battery.svg',width: 18.w,height: 10.h,),
                               SizedBox(width: 7.w,),
                               Image.asset('assets/images/icon_index_page_recording.png',width: 16.w,height: 16.h,)
                             ],
                           )

                         ],
                       ),
                     ],
                   ),
                 ),
                 SizedBox(width: 50.w,),
                 Text('智译',style: AppStyleThemeData.PAGE_TITLE,),
               ],
             ),
             InkWell(
               onTap: () {
                 Navigator.push(
                   Get.context!,
                   CupertinoPageRoute(builder: (context) => NestedSearchScrollPage()),
                 );
                 /*IndexViewController indexViewController = Get.find<IndexViewController>();
                 if (!isHasStartService) {
                   isHasStartService = true;
                   indexViewController.startService();
                 } else {
                   isHasStartService = false;
                   indexViewController.stopService();
                 }*/

               },
               child: Image.asset('assets/images/icon_index_custer_service.png',width: 25.w,height: 25.h),
             )
        ],
      ),
    );


  }

  static bool isHasStartService = false;

  static Widget obtainHeaderForMine(name, email, id, phone, avatar,localPath) {
    double radius = 30.0;
    var domainUrl = CommonUtil.getPicPre();
    return Container(
      padding: EdgeInsets.all(10.w),
      margin: EdgeInsets.only(top: 35.h,left: 10.w),
      width: Get.width,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              // Container(child: SvgPicture.asset('assets/images/icon_default_avatar_l.svg'),width: radius,height: radius, ),
              (localPath.value!=null &&  localPath.value !='')
              ?ClipRRect(borderRadius: BorderRadius.circular(radius),child: Image.file(File(localPath.value.path),fit: BoxFit.cover,width: radius,height: radius,)):
              (avatar !=''?CircleAvatar(backgroundImage:NetworkImage('$domainUrl$avatar')):Container(child: SvgPicture.asset('assets/images/icon_default_avatar_l.svg'),width: radius,height: radius, )),
              // Container(
              //   width: 40.w,
              //   height: 40.h,
              //   child: Image.asset('assets/images/icon_mine_header.png'),
              // ),
              SizedBox(width: 16.w),
              Container(
                margin: EdgeInsets.only(top: 10.h),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(name, style: AppStyleThemeData.normal_bold),
                    Visibility(
                      visible: email != 'null',
                      child: Text(
                        email,
                        style: AppStyleThemeData.HINT_TEXT_STYLE_small,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          InkWell(
            onTap: () {
              if(false){
              //   如果未登录的话，跳转到登录
                Get.toNamed(Routes.LOGIN);
              }else{
                Get.toNamed(
                  Routes.MINE_PROFILE,
                  arguments: {
                    'name': name==null?'':name,
                    'email': email==null?'':email,
                    'id': id,
                    'phone': phone==null?'':phone,
                    'avatar': avatar==null?'':avatar,
                  },
                );
              }

            },
            child: Container(
              color: Colors.red.withValues(alpha: 0),
              padding: EdgeInsets.all(10.w),
              child: Image.asset(
                'assets/images/icon_mine_right_arrow.png',
                width: 24.w,
                height: 24.h,
              ),
            ),
          ),
        ],
      ),
    );


  }


  static Widget obtainHeaderForRealTranslation (DataCallBack dataCallback) {
    return Container(
        padding: EdgeInsets.all(10.w),
        margin: EdgeInsets.only(top: 35.h),
        width: Get.width,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,

          children: [
            Row(
              children: [
                InkWell(
                  onTap: () {
                     // Get.back();
                     dataCallback.businessCallBack!('');
                  },
                  child: Container(
                    padding: EdgeInsets.only(right: 50.w,top: 10.h,bottom: 10.h),
                    child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                  ),
                ),
                SizedBox(width: 72.w),
                GestureDetector(
                  onTap: () {
                     dataCallback.confirmCallBack!('');
                  },
                  child: Text('同声翻译',style: AppStyleThemeData.PAGE_TITLE,),
                )

              ],
            ),
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Get.toNamed(Routes.HISTORY_RECORDS,parameters: {
                      'source':'realTranslation'
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_index_record.png',width: 16.w,height: 16.h,color: AppColors.hint_text_color),
                  ),
                ),
                /*SizedBox(width: 16.w,),
                InkWell(
                  onTap: () {
                    Get.toNamed(Routes.REAL_TIME_LANDSCAPE);
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_realtime_right_header.png',width: 16.w,height: 16.h),
                  ),
                )*/

              ],
            )


          ],
        )
    );


  }

  static Widget obtainHeaderForRealTranslationForLandscape (double screenWith,double screenHeight) {
    return Container(
        padding: EdgeInsets.all(10),
        width: screenHeight,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,

          children: [
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Get.back();
                  },
                  child: Container(
                    padding: EdgeInsets.only(right: 20,top: 13,bottom: 10),
                    child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.h,height: 15.w,),
                  ),
                ),
                SizedBox(width: 312),
                Text('同声翻译',style: AppStyleThemeData.PAGE_TITLE,)
              ],
            ),
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Get.toNamed(Routes.HISTORY_RECORDS,parameters: {
                      'source':'realTranslation'
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_index_record.png',width: 16,height: 16,color: AppColors.hint_text_color),
                  ),
                ),
                SizedBox(width: 16),
                InkWell(
                  onTap: () {
                    Get.back();
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_realtime_right_header.png',width: 16,height: 16),
                  ),
                ),
                SizedBox(width: 27,)

              ],
            )


          ],
        )
    );


  }


  static Widget obtainHeaderForListenTranslation(DataCallBack dataCallback) {
    return Container(
        padding: EdgeInsets.all(10.w),
        margin: EdgeInsets.only(top: 35.h),
        width: Get.width,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,

          children: [
            Row(
              children: [
                InkWell(
                  onTap: () {
                    // Get.back();
                    dataCallback.businessCallBack!('v');
                  },
                  child: Container(
                    color: Colors.red.withValues(alpha: 0),
                    padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
                    child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                  ),
                ),
                SizedBox(width: 112.w),
                GestureDetector(
                  onTap: () {
                    dataCallback.confirmCallBack!('');
                  },
                  child: Text('听译',style: AppStyleThemeData.PAGE_TITLE,),
                )

              ],
            ),
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Get.toNamed(Routes.HISTORY_RECORDS,parameters: {
                      'source':'listeningTranslation'
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_index_record.png',width: 16.w,height: 16.h,color: AppColors.hint_text_color),
                  ),
                ),
                /*SizedBox(width: 8.w,),
                InkWell(
                  onTap: () {
                    Get.toNamed(Routes.LISTENING_TRANS_LANDSCAPE);
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_realtime_right_header.png',width: 16.w,height: 16.h),
                  ),
                )*/

              ],
            )


          ],
        )
    );


  }

  static Widget obtainHeaderForListenTranslationForLandscape (double screenWith,double screenHeight) {
    return Container(
        padding: EdgeInsets.all(10.w),
        margin: EdgeInsets.only(top: 5.h),
        width: screenHeight,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,

          children: [
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Get.back();
                  },
                  child: Container(
                    color: Colors.red.withValues(alpha: 0),
                    padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
                    child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                  ),
                ),
                SizedBox(width: 312.h),
                Text('听译',style: AppStyleThemeData.PAGE_TITLE,)
              ],
            ),
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Get.toNamed(Routes.HISTORY_RECORDS,parameters: {
                      'source':'listeningTranslation'
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_index_record.png',width: 16.w,height: 16.h,color: AppColors.hint_text_color),
                  ),
                ),
                SizedBox(width: 8.w,),
                InkWell(
                  onTap: () {
                    Get.back();
                  },
                  child: Container(
                    padding: EdgeInsets.all(8.w),
                    child: Image.asset('assets/images/icon_realtime_right_header.png',width: 16.w,height: 16.h),
                  ),
                ),
                SizedBox(width: 36.w,),

              ],
            )


          ],
        )
    );


  }

  static Widget obtainHeaderForRecordScript () {
    return Container(
      padding: EdgeInsets.only(left: 20.w,right: 20.w,top: 15.h,bottom: 10.h),
      margin: EdgeInsets.only(top: 35.h),
      width: Get.width,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Container(
                padding: EdgeInsets.only(top: 5.h,bottom: 5.h,left: 5.w,right: 20.h),
                decoration: BoxDecoration(
                  boxShadow: [
                    CommonWidgets.getBoxShadow(),
                  ],
                  color:Colors.white,
                  borderRadius: BorderRadius.circular(32), // Optional: add border radius
                ),
                child: Row(
                  children: [
                    Image.asset('assets/images/product_device1.png',width: 31.w,height: 31.h),
                    SizedBox(width: 10.w,),
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text('智译Pro',style: TextStyle(
                            color: Colors.black,
                            fontSize: 12.sp
                        ),),
                        Row(
                          children: [
                            SvgPicture.asset('assets/images/icon_index_header_battery.svg',width: 18.w,height: 10.h,),
                            SizedBox(width: 7.w,),
                            Image.asset('assets/images/icon_index_page_recording.png',width: 16.w,height: 16.h,)
                          ],
                        )

                      ],
                    ),
                  ],
                ),
              ),
              SizedBox(width: 25.w,),
              Text('录音转写',style: AppStyleThemeData.PAGE_TITLE,),
            ],
          ),

          InkWell(
            onTap: () {
              Get.toNamed(Routes.SEARCH_TRANSCRIPT);
            },
            child: Container(
              padding: EdgeInsets.all(10.w),
              child: Image.asset('assets/images/icon_record_script_searching.png',width: 16.w,height: 16.h),
            ),
          ),
        ],
      ),
    );


  }

  static Widget obtainHeaderForRecordScriptDetail (String audioType,DataCallBack callBack) {
    RecordscriptDetailController recordscriptDetailController =  Get.find<RecordscriptDetailController>();
    return Column(
      children: [
        Container(
            padding: EdgeInsets.all(10.w),
            margin: EdgeInsets.only(top: 45.h),
            width: Get.width,
            child: Row(
              mainAxisAlignment: audioType == 'old' ? MainAxisAlignment.spaceAround: MainAxisAlignment.spaceBetween,
              children: [
                InkWell(
                  onTap: () {
                    if(callBack != null) {
                      callBack!.confirmCallBack!('v');
                    }

                  },
                  child: Container(
                    color: Colors.red.withValues(alpha: 0),
                    child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                    padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
                  ),
                ),

                audioType == 'old' ? Obx(()=> recordscriptDetailController.isHeaderVisible.value? Expanded(child: SizedBox(height: 10.h)): Expanded(child: Container(
                  padding: EdgeInsets.only(top: 5.h, bottom: 5.h,right: 10.h,left: 10.h),
                  decoration: BoxDecoration (
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                      color: Colors.white
                  ),
                  margin: EdgeInsets.only(left: 23.w,right: 23.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      InkWell(
                        onTap: () {
                          RecordscriptDetailController records =  Get.find<RecordscriptDetailController>();
                          if (!records.audioPlay.value) {
                            records.playAudioFromDeviceFile();
                          } else {
                            records.pauseAudioFromDeviceFile();
                          }

                        },
                        child: !recordscriptDetailController.audioPlay.value? Image.asset('assets/images/icon_audio_begin_starting.png',width: 24.w,height: 24.h,):
                        Image.asset('assets/images/icon_play_realtime_btn_running.png',width: 24.w,height: 24.h,),
                      ),
                      /*Expanded(child: Image.asset('assets/images/icon_audio_wave_realtime.png', height: 24.h,))*/
                      Expanded(child: AudioFileWaveforms(
                        waveformType: WaveformType.fitWidth,
                        size: Size(Get.width, 20.h),
                        playerController: recordscriptDetailController.playerController,
                        playerWaveStyle: PlayerWaveStyle(
                            fixedWaveColor: Colors.grey,
                            liveWaveColor: AppColors.app_base_theme_color,
                            spacing: 6
                        ),
                        padding: EdgeInsets.only(right: 10.w),
                      ))
                    ],
                  ),
                ))):SizedBox(),

                Row(
                  children: [
                    audioType=='new' ? InkWell(
                      onTap: () {
                        callBack!.businessCallBack!('v');
                      },
                      child: Container(
                        padding: EdgeInsets.all(10.w),
                        child: Image.asset('assets/images/icon_hover_text_fields.png',width: 16.w,height: 16.h,),
                      ),
                    ):SizedBox(),
                    audioType=='new' ? SizedBox(width: 5.w,):SizedBox(),
                   /* audioType=='new' ? Container(
                      padding: EdgeInsets.all(10.w),
                      child: Image.asset('assets/images/icon_record_scritpt_landmode.png',width: 16.w,height: 16.h,),
                    ): SizedBox(),*/
                    audioType=='old' ? InkWell(
                      onTap: () {
                        RecordscriptDetailController records =  Get.find<RecordscriptDetailController>();
                        records.showpoupWindowExport(Get.context!);
                      },
                      child: Container(
                        padding: EdgeInsets.all(10.w),
                        child: Image.asset('assets/images/icon_audio_upload.png',width: 16.w,height: 16.h,),
                      ),
                    ): SizedBox(),
                    SizedBox(width: 5.w,),
                    InkWell(
                      onTap: () {
                        RecordscriptDetailController records =  Get.find<RecordscriptDetailController>();
                        records.showpoupWindowMore(Get.context!);
                      },
                      child: Container(
                        padding: EdgeInsets.all(10.w),
                        child: Image.asset('assets/images/icon_record_more_action.png',width: 16.w,height: 16.h,),
                      ),
                    )

                  ],
                )


              ],
            )
        )
      ],
    );


  }


  static Widget obtainHeaderForEditorScript () {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: EdgeInsets.all(10.w),
          margin: EdgeInsets.only(top: 45.h),
          width: Get.width,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                  padding: EdgeInsets.all(10.w),
                  width: 80.w,
                  child: Row(
                    children: [
                      InkWell(
                        onTap: () {
                          Get.back();
                        },
                        child: Container(
                          color: Colors.red.withValues(alpha: 0),
                          child: SvgPicture.asset('assets/images/app_header_back_btn.svg',width: 8.w,height: 15.h,),
                          padding: EdgeInsets.only(right: 20.w,top: 10.h,bottom: 10.h),
                        ),
                      )
                    ],
                  )
              ),
              /*InkWell(
                onTap: () {
                    if (dataCallBack != null) {
                      dataCallBack!.businessCallBack!('');
                    }
                },
                child: Container(
                  child: Column(
                    children: [
                      Image.asset('assets/images/icon_script_bottom_menu_save.png',width: 24.w,height: 24.h,),
                      SizedBox(height: 6.h),
                      Text('保存')
                    ],
                  ),
                ),
              )*/

            ],
          ),
        )
      ],
    );


  }
}

